﻿#include "TreeWidgetGroup.h"
#include "TreeItemTypes.h"
#include "ui_TreeWidgetGroupWin.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsGroupData.h"

#include "FITK_Kernel/FITKCore/FITKDataRepo.h"
#include "FITK_Kernel/FITKCore/FITKOperatorRepo.h"
#include "FITK_Kernel/FITKCore/FITKActionOperator.h"

#include <QMenu>

#define _DATAOBJTYPECOLUMN_ 0
#define _DATAOBJTYPE_ 3
#define _DATATYPECOLUMN_ 1
#define _DATATYPE_ 4
#define _DATAOBJIDCOLUMN_ 2
#define _DATAOBJID_ 5

namespace GUI
{
    TreeWidgetGroup::TreeWidgetGroup(QWidget* parent /*= nullptr*/)
        :TreeWidgetPage(parent)
    {
        this->blockSignals(true);
        this->initTree();
        this->expandAll();
        this->blockSignals(false);
    }

    TreeWidgetType TreeWidgetGroup::getTreeWidgetType()
    {
        return PageGroup;
    }

    void TreeWidgetGroup::updateGroupTreeItemState()
    {
        // 获取Group根节点
        int rootCount = this->topLevelItemCount();
        if (rootCount == 0) return;

        // 遍历根节点
        for (int i = 0; i < rootCount; ++i)
        {
            QTreeWidgetItem* rootItem = this->topLevelItem(i);
            if (!rootItem) continue;

            // 更新根节点的状态
            int rootObjID = rootItem->data(_DATAOBJIDCOLUMN_, _DATAOBJID_).toInt();
            Core::FITKAbstractNDataObject* rootData = FITKDATAREPO->getTDataByID<Core::FITKAbstractNDataObject>(rootObjID);
            if (rootData)
            {
                bool rootState = rootData->isEnable();
                rootItem->setCheckState(0, rootState ? Qt::Checked : Qt::Unchecked);
            }

            // 遍历组节点
            for (int j = 0; j < rootItem->childCount(); ++j)
            {
                QTreeWidgetItem* groupItem = rootItem->child(j);
                if (!groupItem) continue;

                // 更新组节点的状态
                int groupObjID = groupItem->data(_DATAOBJIDCOLUMN_, _DATAOBJID_).toInt();
                Core::FITKAbstractNDataObject* groupData = FITKDATAREPO->getTDataByID<Core::FITKAbstractNDataObject>(groupObjID);
                if (groupData)
                {
                    bool groupState = groupData->isEnable();
                    groupItem->setCheckState(0, groupState ? Qt::Checked : Qt::Unchecked);
                }

                // 遍历子节点
                for (int k = 0; k < groupItem->childCount(); ++k)
                {
                    QTreeWidgetItem* dataItem = groupItem->child(k);
                    if (!dataItem) continue;

                    // 更新子节点的状态
                    int dataObjID = dataItem->data(_DATAOBJIDCOLUMN_, _DATAOBJID_).toInt();
                    Core::FITKAbstractNDataObject* data = FITKDATAREPO->getTDataByID<Core::FITKAbstractNDataObject>(dataObjID);
                    if (data)
                    {
                        bool dataState = data->isEnable();
                        dataItem->setCheckState(0, dataState ? Qt::Checked : Qt::Unchecked);
                    }
                }
            }
        }
    }

    void TreeWidgetGroup::updateNodeName(int objID)
    {
        //获取数据名称
        Core::FITKAbstractNDataObject* data = FITKDATAREPO->getTDataByID<Core::FITKAbstractNDataObject>(objID);
        if (!data) return;
        QString name = data->getDataObjectName();
        //对应数据的树节点
        QTreeWidgetItem* item = nullptr;
        for (int i = 0; i < this->topLevelItemCount(); ++i)
        {
            QTreeWidgetItem * root = this->topLevelItem(i);
            if (!root)continue;
            item = this->getItemByObjID(objID, root);
            if (item) break;
        }
        if (!item) return;
        item->setText(0, name);
    }

    void TreeWidgetGroup::updateGroupTree()
    {
        this->blockSignals(true);
        this->clear();
        //获取组管理器
        Acoustics::FITKAcousticsGroupManager* groupManager = Acoustics::FITKAcousticsGroupManager::GetGroupManagerFromFrame();
        if (!groupManager) return;
        bool stateGroupRoot = groupManager->isEnable();
        //Group根节点
        QTreeWidgetItem* groupTreeRoot = this->CreateTreeRootItem(this, tr("Group"), GroupTreeItemsType::GTGroupRoot, "", stateGroupRoot);
        groupTreeRoot->setData(_DATAOBJIDCOLUMN_, _DATAOBJID_, groupManager->getDataObjectID());
        //获取数量
        int count = groupManager->getDataCount();
        for (int i = 0; i < count; ++i)
        {
            //获取组数据
            Acoustics::FITKAcousticsGroupData* groupObj = groupManager->getDataByIndex(i);
            if (!groupObj) continue;
            //获取组ID, 名称和状态
            int groupDataID = groupObj->getDataObjectID();
            QString groupName = groupObj->getDataObjectName();
            bool stateGroup = groupObj->isEnable();
            //创建组节点
            QTreeWidgetItem* groupTreeNode = this->CreateTreeItem(groupTreeRoot, groupName, GroupTreeItemsType::GTGroup, "", stateGroup);
            groupTreeNode->setData(_DATAOBJIDCOLUMN_, _DATAOBJID_, groupDataID);
            //获取组管理的数据数量
            int dataCount = groupObj->getDataCount();
            for (int j = 0; j < dataCount; ++j)
            {
                //获取数据
                Core::FITKAbstractNDataObject* data = groupObj->getGroupDataObjAt(j);
                Acoustics::FITKAcousticsGroupData::AcousticsGroupData groupData = groupObj->getGroupDataAt(j);
                if (!data) continue;
                //获取数据
                bool state = data->isEnable();
                QString name = data->getDataObjectName();
                int objID = groupData._objID;
                int objType = groupData._objType;
                int type = groupData._type;
                //创建组节点
                QTreeWidgetItem* dataTreeNode = this->CreateTreeItem(groupTreeNode, name, GroupTreeItemsType::GTGroupChild, "", state);
                dataTreeNode->setData(_DATAOBJTYPECOLUMN_, _DATAOBJTYPE_, objType);
                dataTreeNode->setData(_DATATYPECOLUMN_, _DATATYPE_, type);
                dataTreeNode->setData(_DATAOBJIDCOLUMN_, _DATAOBJID_, objID);
            }
        }
        this->blockSignals(false);
    }

    void TreeWidgetGroup::initTree()
    {
        //获取组管理器
        Acoustics::FITKAcousticsGroupManager* groupManager = Acoustics::FITKAcousticsGroupManager::GetGroupManagerFromFrame();
        if (!groupManager) return;
        bool stateGroupRoot = groupManager->isEnable();
        //Group根节点
        QTreeWidgetItem* groupRoot = this->CreateTreeRootItem(this, tr("Group"), GroupTreeItemsType::GTGroupRoot, "", stateGroupRoot);
        groupRoot->setData(_DATAOBJIDCOLUMN_, _DATAOBJID_, groupManager->getDataObjectID());
    }

    void TreeWidgetGroup::on_itemDoubleClicked(QTreeWidgetItem *item, int column)
    {

    }

    void TreeWidgetGroup::on_itemStateChanged(QTreeWidgetItem *item, bool state, int column)
    {        
        QString operName = "";
        // 根据 state 决定操作器
        if (state)
            operName = "actionGroupShow";
        else
            operName = "actionGroupHide";

        // 创建一个QObject对象
        QObject* sender = new QObject;
        sender->setObjectName(operName);
        
        // 从仓库中获取指定名称的操作器
        Core::FITKActionOperator* oper = Core::FITKOperatorRepo::getInstance()->getOperatorT<Core::FITKActionOperator>(operName);
        if (oper == nullptr) return;

        QList<QTreeWidgetItem*> items;
        items.append(item);
        // 调用操作器执行操作
        oper->setEmitter(sender);
        oper->setArgs("SelectedItems", QVariant::fromValue((void*)(&items)));
        oper->actionTriggered();

        // 释放QObject对象
        delete sender;
    }

    void TreeWidgetGroup::on_itemContextMenu(const QList<QTreeWidgetItem*>& item, QMenu* menu)
    {
        if (item.isEmpty() || !menu) return;
        //获取数量和类型
        int num = 0;
        GroupTreeItemsType type = GroupTreeItemsType::GTNone;
        for (QList<QTreeWidgetItem*>::const_iterator iter = item.begin(); iter != item.end(); ++iter)
        {
            if (!(*iter))continue;
            type = GroupTreeItemsType((*iter)->type());
            ++num;
        }
        //创建动作
        if (num == 1)
        {
            if (type == GroupTreeItemsType::GTGroupRoot)
                this->menuAddAction(menu, "actionCreateGroup", tr("Create Group"));
            else
                this->menuAddAction(menu, "actionGroupEdit", tr("Edit"));
        }
        this->menuAddAction(menu, "actionGroupShow", tr("Show"));
        this->menuAddAction(menu, "actionGroupHide", tr("Hide"));
        this->menuAddAction(menu, "actionGroupShowOnly", tr("ShowOnly"));
    }

    void TreeWidgetGroup::menuAddAction(QMenu* menu, QString objName, QString textName, QString iconFileName)
    {
        if (!menu) return;
        //菜单栏添加工具
       QAction* action = menu->addAction(QIcon(iconFileName), textName);
        action->setObjectName(objName);
    }

    QTreeWidgetItem* TreeWidgetGroup::getItemByObjID(int objID, QTreeWidgetItem * item)
    {
        if (!item) return nullptr;
        if (item->data(_DATAOBJIDCOLUMN_, _DATAOBJID_).toInt() == objID)
            return item;
        for (int i = 0; i < item->childCount(); ++i)
        {
            QTreeWidgetItem * itemChild = item->child(i);
            if (!itemChild)continue;
            QTreeWidgetItem * nodeItem = getItemByObjID(objID, itemChild);
            if (nodeItem) return nodeItem;
        }
        return nullptr;
    }

    TreeWidgetGroupWin::TreeWidgetGroupWin(QWidget* parent /*= nullptr*/)
        :Core::FITKWidget(parent)
    {
        _ui = new Ui::TreeWidgetGroupWin;
        _ui->setupUi(this);

        _tree = new TreeWidgetGroup(parent);
        _ui->TreeLayout->addWidget(_tree);
    }


    TreeWidgetGroupWin::~TreeWidgetGroupWin()
    {
        if (_ui) delete _ui;
    }

    GUI::TreeWidgetGroup* TreeWidgetGroupWin::getTreeWidget()
    {
        return _tree;
    }
}


